# -*- coding: utf-8 -*-

'''eof
name:申请人循环贷账户近6个月平均应还款
code:IC48_Ind6MonsRecycleAvgRepay
tableName:
columnName:
groups:个人征信
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:申请人循环贷账户近6个月平均应还款
eof'''

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

# 获取表信息方法
def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

# 数值判断方法
def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def IC48_Ind6MonsRecycleAvgRepay():
    try:
        data = IC_DSJ_2D['data']
        # 信贷交易信息概要
        PCO = getTable(data, 'PCO', 1, 0)
        if PCO == [{}]:
            return u'缺失值'
        elif PCO == 0:
            return 0
        else:
            # 信贷交易信息概要信息单元
            PC02 = getTable(PCO[0], 'PC02', 0, 0)
            if PC02 == 0:
                return 0
            elif PC02 == [{}]:
                return u'缺失值'
            else:
                # 循环贷账户汇总信息段
                PC02G = getTable(PC02[0], 'PC02G', 0, 1)
                if PC02G == 0:
                    return 0
                elif PC02G == [{}]:
                    return u'缺失值'
                else:
                    # 最近 6 个月平均应还款
                    PC02GJ03 = PC02G[0].get('PC02GJ03')
                    if isfloat(PC02GJ03):
                        return float(PC02GJ03)
                    else:
                        return u'缺失值'
    except:
        return u'缺失值'



result = IC48_Ind6MonsRecycleAvgRepay()

